//
//  DetailsViewController.m
//  UI__相册三代版
//
//  Created by dllo on 16/4/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DetailsViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface DetailsViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation DetailsViewController

-(void)dealloc{
    [_name release];
    [_number release];
    [_imageButton release];
    [_myImage release];
    [_model release];
    [_index release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.imageButton.backgroundColor = [UIColor cyanColor];
    self.imageButton.frame = CGRectMake(WIDTH / 2 - 50, 100, 100, 100);
    [self.view addSubview:self.imageButton];
    
    //(不要)
//    [self.imageButton addTarget:self action:@selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //改成圆的
    self.imageButton.layer.masksToBounds = YES;
    self.imageButton.layer.cornerRadius = 50;
    
    self.name = [[UITextField alloc]initWithFrame:CGRectMake(WIDTH / 2 - 100, 250, 200, 50)];
    self.name.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.name];
    [_name release];
    
    self.number = [[UITextField alloc]initWithFrame:CGRectMake(WIDTH / 2 - 100, 350, 200, 50)];
    self.number.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.number];
    [_number release];

    //获取系统相册
    UIImagePickerController *controll = [[UIImagePickerController alloc]init];
    controll.sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
    controll.allowsEditing = YES;
    controll.navigationBar.barStyle = UIBarStyleBlack;
    [controll.navigationBar setTintColor:[UIColor whiteColor]];
    
    //(不要这个, 有这个就直接调到系统图片那个界面了)
//    controll.delegate = self;
//    [self presentViewController:controll animated:YES completion:^{
//        
//        
//    }];
    
    [controll release];
    
//不点编辑按钮,就不编辑
    self.imageButton.userInteractionEnabled = NO;
    self.name.userInteractionEnabled = NO;
    self.number.userInteractionEnabled = NO;
    

    [self.imageButton setBackgroundImage:self.model.image forState:UIControlStateNormal];
    self.name.text = self.model.name;
    self.number.text = self.model.number;

    self.saveButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.saveButton.backgroundColor = [UIColor redColor];
    self.saveButton.frame = CGRectMake(WIDTH / 2 - 50, 410, 100, 100);
    [self.view addSubview:self.saveButton];
    [self.saveButton addTarget:self action:@selector(saveButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
  
    
    self.saveButton.userInteractionEnabled = NO;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
    
    
    

    // Do any additional setup after loading the view.
}

-(void)rightButtonAction{
    self.imageButton.userInteractionEnabled = YES;
    self.name.userInteractionEnabled = YES;
    self.number.userInteractionEnabled = YES;
    self.saveButton.backgroundColor = [UIColor redColor];
    
}


-(void)saveButtonAction{

    //调用block
    ListModel *model = [[ListModel alloc]init];
    
    if (self.myImage != nil) {
        model.image = self.myImage;
    }else{
    
        model.image = self.model.image;
        NSLog(@"1122");//不好用
    }
    
    model.name = self.name.text;
    model.number = self.number.text;
    self.myBlock(model, self.index);
    
    //保存并跳出
    [self.navigationController popViewControllerAnimated:YES];
   
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
